#include "stdafx.h"
#include "file_manager.h"

namespace io
{

	CFileManager* CFileManager::_inst = 0;

#undef UNICODE

	unsigned char * CFileManager::_get_data_from_file(const char * filename, DWORD * out_sz)
	{
		HANDLE hfile;
		DWORD sz_read;
		DWORD sz_file;
		BOOL ret;
		unsigned char * data;

		if ((hfile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
			goto err1;

		if ((sz_file = GetFileSize(hfile, NULL)) == INVALID_FILE_SIZE)
			goto err2;

		data = new unsigned char[sz_file];
		if (data == NULL)
			goto err2;

		if((ret = ReadFile(hfile, (LPVOID)data, sz_file, &sz_read, NULL)) == FALSE)
			goto err3;

		CloseHandle(hfile);
		if (out_sz != NULL)
			*out_sz = sz_file;
		return data;

err3:
		delete [] data;
err2:
		CloseHandle(hfile);
err1:
		return NULL;
	}
} // namespace io